<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\facade\Request;
use Yansongda\Pay\Log;
use Yansongda\Pay\Pay;

class Book extends Controller
{
    protected $url;
    protected $config;
    public function __construct()
    {
        parent::__construct();
        $this->url=Request::instance()->domain();
        $this->config=[
            'app_id' => '2019092867865728',
            'notify_url' => $this->url."/index/book/notify_url",
            'return_url' =>$this->url."/index/book/return_url",
            //'ali_public_key' => 'MIIEogIBAAKCAQEAkdKLQSIkTJkjEtcmpwENHpPYwqjS1mdw+6TAGTCeYGvoXUgJ4l+VdoVDpt7aCpprLCD2fWM2hidEcbICwig9zE4RlvUUwqqXjwAQVCY+3YB1ct87vSIA0Oc3gLwZi6paQc550OEMHXfmK4IBlvwhL4WvjF08DAmwJcgj4ZPGT/Njj8qH+Nl8DIvWvqUmm0NnWPgiVSQkIZMAyVndRSbTV/idKeOk/AzhHxuiUGcnyQxcmlBFEnO6WbT8LvM8IK3v1r1nd1eOlYwLcpIdQjAgFKXVgDaBYdvK5w2fnAK0TkjVxwGAwm1MGAVpzjwY4z5GicXVWORwqp5WltYemtyjjQIDAQABAoIBAG4pZrYv+sg7MDn9e9/SdXzSKID2VUFQKBSTY+QjzKH/Iy3FTGJN33YIOxGHbaVw/uH5jP0wxHIYnCZgWqEfEYCUwckPIT3m7lRDshJASmG/t/quFk1NsNwgsNd6Qro2WbU5LOHWAfcJK0NXFahglRl3nyUlEkXt07rXNfQmVTzz9iV37Qpf+xoWrqY/9Qu62oF1WiFBcHkOynMQAEzjn4spZ55HwxFuH42ZrPQMGqIQ8rySjq25NRGc1Nny7JWtod/emNBAJiSk6Xfb7Tm4zEwyMzNIYghOySNRDw9VKOCgeBpJqOYs+sFV+3SM/qE6qQpeet3Pe0bRFmLSDV9lIykCgYEA2T50ZCHhFqSy6VK0IlQI1v/hM5zAYa/dT1ZTLMY00XpWigiFCHm1vksN5j4oBlQcGnS285qiO/td4HFmKWaMIEUyE2eIKRoSVa7+0zreJDc2JdhuxisL2t32DKMCL1v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lic3PdeVh7fq5/K8hqCPmfReNk97ilrT/uTXc3R4TUwpyjXAoGAHPZA4a3VKhotbKVwSs7fNRk2dXrvs6VxigqZLr5qSTbD0/5dfd1I7E+DJcm7/0Bvqw6YwIg2X0o58JgEYC9M4AHQyZs7CFl7EbWZ5ZtQCND+WTca3MP6VQ1U1uvFZuhMhCm2c+L8Ko6SCafT7O9A7eHUgSJgef6/hszYgdZH9ek=',// 使用公钥证书模式，请配置下面两个参数，同时修改ali_public_key为以.crt结尾的支付宝公钥证书路径，如（./cert/alipayCertPublicKey_RSA2.crt）
            // 'app_cert_public_key' => './cert/appCertPublicKey.crt', //应用公钥证书路径
            // 'alipay_root_cert' => './cert/alipayRootCert.crt', //支付宝根证书路径
            'log' => [ // optional
                'file' => './logs/alipay.log',
                'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
                'type' => 'single', // optional, 可选 daily.
                'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
            ],
            'http' => [ // optional
                'timeout' => 5.0,
                'connect_timeout' => 5.0,
                // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
            ],
            // optional,设置此参数，将进入沙箱模式
        ];
    }

    public function index(){
        $bookList=Db::name('book')->select();
        $this->assign('bookList',$bookList);
        return $this->fetch();
    }
    public function buy(){
        $bookId=input('param.book_id');
        $book=Db::name('book')->find($bookId);
        $this->assign('book',$book);
        return $this->fetch();
    }
    public function pay(){
        $userId=session('user_id');
        $checkAdderss=Db::name('usermsg')->where('usermsg_user_id',$userId)->find();
        if(!$checkAdderss){
            $this->redirect('index/center/index',['buybook'=>1]);
        }
        $bookId=input('param.book_id');
        $book=Db::name('book')->find($bookId);
        $userId=session('user_id');
        $payData['pay_out_trade_no']='MHK'.time().rand(1000,9999);
        $payData['pay_user_id']=$userId;
        $payData['pay_book_id']=$bookId;
        $payData['pay_price']=$book['book_price'];
        $payData['pay_payed']=0;
        $payData['pay_pay_time']=time();
        $payData['pay_express']=0;
        $payData['pay_state']=0;
        $save=Db::name('pay')->insert($payData);
        if($save){
            $order = [
                'out_trade_no' => $payData['pay_out_trade_no'],
                'total_amount' => $payData['pay_price'],
                'subject' => '图书购买',
            ];
        }
        $alipay = Pay::alipay($this->config)->web($order);

        return $alipay->send();// laravel 框架中请直接 `return $alipay`

    }
    public function return_url()
    {

        $this->redirect('index/center/index');
    }

    public function notify_url()
    {
            $pay_out_trade_no=input('param.out_trade_no');
            $res=Db::name('pay')->where('pay_out_trade_no',$pay_out_trade_no)->update(['pay_payed'=>1]);
            if($res){

            }
    }


}

